package tankgame;

import java.io.*;
import java.util.Vector;

/**
 * @author: lizhiwen
 * @version: 1.0
 */
public class Record {
    private static int hitTankNum = 0;
    private static FileWriter fileWriter = null;
    private static BufferedWriter bufferedWriter = null;
    private static BufferedReader bufferedReader = null;
    private static String recordFile = "src\\score";
    private static Vector<EnemyTank> enemyTanks = null;
    private static Vector<Node> nodes = new Vector<>();
    private static Vector<Wall> walls = null;
    private static Vector<WallNode> wallNodes = new Vector<>();
    private static MyTank myTank = null;

    public static MyTank getMyTank() {
        return myTank;
    }

    public static void setMyTank(MyTank myTank) {
        Record.myTank = myTank;
    }

    public static Vector<Node> getNodes() {
        return nodes;
    }

    public static void setNodes(Vector<Node> nodes) {
        Record.nodes = nodes;
    }

    public static int getHitTankNum() {
        return hitTankNum;
    }

    public static void setHitTankNum(int hitTankNum) {
        Record.hitTankNum = hitTankNum;
    }

    public static Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Record.enemyTanks = enemyTanks;
    }

    public static String getRecordFile() {
        return recordFile;
    }

    public static Vector<Wall> getWalls() {
        return walls;
    }

    public static void setWalls(Vector<Wall> walls) {
        Record.walls = walls;
    }

    public static Vector<WallNode> getWallNodes() {
        return wallNodes;
    }

    public static void setWallNodes(Vector<WallNode> wallNodes) {
        Record.wallNodes = wallNodes;
    }

    public static void add(){
        Record.hitTankNum++;
    }

    //拷贝击杀数量以及敌方坦克信息
    public static void keepRecord(){
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(recordFile));
            //第一行写击杀坦克数
            bufferedWriter.write(hitTankNum + "\r\n");
            //第二行记录我方坦克
            if (myTank.isLive){
                String myTankRecord = myTank.getX() + " " + myTank.getY() + " " + myTank.getDirect();
                bufferedWriter.write(myTankRecord + "\r\n");
            }
            //后面记录敌方坦克
            for (EnemyTank enemyTank : enemyTanks) {
                if (enemyTank.isLive){
                    String tankRecord = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect();
                    bufferedWriter.write(tankRecord + "\r\n");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (bufferedWriter != null){
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    //拷贝墙
    public static void keepWallRecord(){
        try {
            bufferedWriter = new BufferedWriter(new FileWriter("src\\wallRecord"));
            //记录墙
            for (Wall wall : walls) {
                if (wall.isLive){
                    String wallRecord = wall.x + " " + wall.y;
                    bufferedWriter.write(wallRecord + "\r\n");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (bufferedWriter != null){
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //通过一系列操作返回敌方坦克信息
    public static Vector<Node> getNodeAndScore(){
        try {
            bufferedReader = new BufferedReader(new FileReader(recordFile));
            //恢复击杀数
            hitTankNum = Integer.parseInt(bufferedReader.readLine());
            String line = "";
            //恢复我方坦克
            line = bufferedReader.readLine();
            String part2[] = line.split(" ");
            myTank = new MyTank(Integer.parseInt(part2[0]), Integer.parseInt(part2[1]));
            myTank.setDirect(Integer.parseInt(part2[2]));
            //恢复敌方坦克
            while ((line = bufferedReader.readLine()) != null){
                String part[] = line.split(" ");
                nodes.add(new Node(Integer.parseInt(part[0]),Integer.parseInt(part[1]), Integer.parseInt(part[2])));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return nodes;
    }

    //通过一系列操作返回墙信息
    public static Vector<WallNode> getWallNodeAndScore(){
        try {
            bufferedReader = new BufferedReader(new FileReader("src\\wallRecord"));
            String line = "";
            while ((line = bufferedReader.readLine()) != null){
                String part[] = line.split(" ");
                wallNodes.add(new WallNode(Integer.parseInt(part[0]),Integer.parseInt(part[1])));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return wallNodes;
    }
}
